/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.td.engine.entity;

import com.badlogic.gdx.assets.AssetManager;
import com.badlogic.gdx.graphics.g2d.*;
import org.td.Application;

import java.util.HashMap;
import java.util.Map;

/**
 *
 * @author Администратор
 */
public class EntitySprite {
    
    private Map<String, Animation> animationMap;
    private float time;
    
    private Animation currentAnimation;
    private Sprite sprite;
    
    public EntitySprite(String texture) {
        sprite = new Sprite();

        animationMap = new HashMap<String, Animation>();
        AssetManager asm = Application.getAssetManager();
        TextureAtlas atlas = asm.get("assets/assets.atlas", TextureAtlas.class);


        TextureRegion reg = atlas.findRegion(texture);//new TextureRegion(texture);
        animationMap.put("stand", new Animation(0.25f, reg));
        setAnimation("stand");

    }
    
    public void setAnimation(String name) {
        currentAnimation = animationMap.get(name);
    }

    public int getWidth() {
        return currentAnimation.getKeyFrame(time, true).getRegionWidth();
    }

    public int getHeight() {
        return currentAnimation.getKeyFrame(time, true).getRegionHeight();
    }

    public void setRotation(float degrees) {
        sprite.setRotation(degrees);
    }
    
    public void render(SpriteBatch batch, float x, float y, boolean center) {
        if (currentAnimation != null) {
            TextureRegion frame = currentAnimation.getKeyFrame(time, true);


            sprite.setRegion(frame);
            if (center) {
                sprite.setOrigin(frame.getRegionWidth() / 2.0f, frame.getRegionHeight() / 2.0f);
                sprite.setPosition(x - frame.getRegionWidth() / 2.0f, y - frame.getRegionHeight() / 2.0f);
            } else {
                sprite.setOrigin(0f, 0f);
                sprite.setPosition(x, y);
            }
            sprite.setSize(frame.getRegionWidth(), frame.getRegionHeight());

            sprite.draw(batch);

            //batch.draw(frame, x - frame.getRegionWidth() / 2.0f, y - frame.getRegionHeight() / 2.0f);
        }
    }
    
    public void update(float time) {
        this.time += time;
    }
}
